package com.hb.hb_guestbook;

import java.sql.*;
import java.util.ArrayList;
import com.wonders.ips.common.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GuestBook {

  public int id;
  public String username;
  public String title;
  public String content;
  public String pub_date;
  public int pid;
  public int del_flag;
  public String publish_flag;

  private String sql = "";

  public GuestBook() {
  }

  public void add(String s_username, String s_title, String s_content,int s_pid,int s_del_flag,String s_publish_flag) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
    try {
      dbcon = new DbConnection();
      conn = dbcon.getCon();

      //int i_id = dbcon.getMaxId("tb_guestbook");

      sql = "insert into tb_guestbook (username,title,content,pub_date,pid,del_flag,publish_flag) " +
            "values (?,?,?,NOW(),?,?,?)";
      Timestamp   t   =   new   Timestamp(new   java.util.Date().getTime());
      pstmt = conn.prepareStatement(sql);

      //pstmt.setInt(1,i_id);
      pstmt.setString(1,s_username);
      pstmt.setString(2,s_title);
      pstmt.setString(3,s_content);
	  pstmt.setInt(4,s_pid);
	  pstmt.setInt(5,s_del_flag);
	  pstmt.setString(6,s_publish_flag);
      pstmt.executeUpdate();

    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
        try {if (pstmt!=null) pstmt.close();}
        catch (Exception e) {}
        try {if (conn!=null) conn.close();}
        catch (Exception e) {}
    }

  }

  public ArrayList list(String sWhere) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
    ArrayList ary = new ArrayList();
    try {
      dbcon = new DbConnection();
      conn = dbcon.getCon();
      stmt = conn.createStatement();
      sql = "select * from tb_guestbook " + sWhere;
      rs = stmt.executeQuery(sql);

      while (rs.next()) {
	      GuestBook guestbook = new GuestBook();
        guestbook.id = rs.getInt("id");
        guestbook.username = rs.getString("username");
        guestbook.title = rs.getString("title");
        guestbook.content = rs.getString("content");
        guestbook.pub_date = rs.getDate("pub_date").toString();
		guestbook.pid = rs.getInt("pid");
		guestbook.del_flag = rs.getInt("del_flag");
		guestbook.publish_flag = rs.getString("publish_flag");
	    ary.add(guestbook);
      }
    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
		try {if (rs!=null) rs.close();}
        catch (Exception e) {}
        try {if (stmt!=null) stmt.close();}
        catch (Exception e) {}
        try {if (conn!=null) conn.close();}
        catch (Exception e) {}
    }
    return ary;
  }

  public void del(int i_id) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	
    try {
      dbcon = new DbConnection();
      conn = dbcon.getCon();
      sql = "update tb_guestbook set del_flag=1 where id=? ";
      pstmt = conn.prepareStatement(sql);

      pstmt.setInt(1,i_id);
      pstmt.executeUpdate();

    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
        try {if (pstmt!=null) pstmt.close();}
        catch (Exception e) {}
        try {if (conn!=null) conn.close();}
        catch (Exception e) {}
    }
  }
  
  public void publish(int i_id,String s_publish_flag) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	
    try {
      dbcon = new DbConnection();
      conn = dbcon.getCon();
      sql = "update tb_guestbook set publish_flag='"+s_publish_flag+"' where id='"+i_id+"' ";
      pstmt = conn.prepareStatement(sql);

    
	  
      pstmt.executeUpdate();

    } catch (Exception e) {
      System.err.println(e.getMessage());
    } finally {
        try {if (pstmt!=null) pstmt.close();}
        catch (Exception e) {}
        try {if (conn!=null) conn.close();}
        catch (Exception e) {}
    }
  }

  public void getDetial(String sWhere) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	try {
	  dbcon = new DbConnection();
	  conn = dbcon.getCon();
	  stmt = conn.createStatement();
	  sql = "select * from tb_guestbook " + sWhere;
	  System.out.println(sql);
	  rs = stmt.executeQuery(sql);
	  //GuestBook guestbook=null;
	  if(rs.next()) {
		//guestbook = new GuestBook();
		this.id = rs.getInt("id");
		this.username = rs.getString("username");
		System.out.println("username="+username);
		this.title = rs.getString("title");
		this.content = rs.getString("content");
		this.pub_date = rs.getDate("pub_date").toString();
		System.out.println("pub_date="+pub_date);
		this.pid = rs.getInt("pid");
		this.del_flag = rs.getInt("del_flag");
		this.publish_flag = rs.getString("publish_flag");
	  }
	} catch (Exception e) {
	  System.err.println(e.getMessage());
	} finally {
		try {if (rs!=null) rs.close();}
		catch (Exception e) {}
		try {if (stmt!=null) stmt.close();}
		catch (Exception e) {}
		try {if (conn!=null) conn.close();}
		catch (Exception e) {}
	}
	/*return guestbook;*/
  }

  public void update(int i_id,String s_username, String s_title, String s_content) {
	DbConnection dbcon = null;
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	
	try {
	  dbcon = new DbConnection();
	  conn = dbcon.getCon();

	  sql ="update tb_guestbook set username=?,title=?,content=?,pub_date=getDate() where id=?";

	  pstmt = conn.prepareStatement(sql);

	  pstmt.setString(1,s_username);
	  pstmt.setString(2,s_title);
	  pstmt.setString(3,s_content);
	  pstmt.setInt(4,i_id);
	  pstmt.executeUpdate();

	} catch (Exception e) {
	  System.err.println(e.getMessage());
	} finally {
		try {if (pstmt!=null) pstmt.close();}
		catch (Exception e) {}
		try {if (conn!=null) conn.close();}
		catch (Exception e) {}
	}

  }

  public static void main(String[] args) {

  }


}
